package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.SysRole;
import com.moji.entity.SysUser;
import com.moji.entity.dto.PrOnOffDto;
import com.moji.entity.dto.RoleSelectBoxDto;
import com.moji.entity.dto.TreeDto;
import com.moji.enums.MoJiEnumConfig;
import com.moji.mapper.SysRoleMapper;
import com.moji.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.service.SysUserService;
import com.moji.util.MoJiCommonUtils;
import com.moji.util.RoleTreeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-07-23
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysUserService sysUserService;

    @Override
    public boolean add(SysRole record) {
        //所属部门不能为空
        if(org.apache.commons.lang3.StringUtils.isEmpty(record.getDeptCode())){
            throw new RuntimeException("所属部门不能为空,请核查!");
        }
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(SysRole::getRoleCode);
        List<SysRole> roleList = this.list(wrapper);
        if(!roleList.isEmpty()){
            String roleCode = String.format("%06d",Integer.parseInt(roleList.get(0).getRoleCode())+1);
            record.setRoleCode(roleCode);
        }else{
            String roleCode = String.format("%06d",1);
            record.setRoleCode(roleCode);
        }
        record.setPrOnoff(Integer.parseInt(MoJiEnumConfig.CCP_SYSTEM_PR_ON_OFF.getCode()));
        record.setOrderNo(ObjectUtil.isEmpty(record.getOrderNo())?1:record.getOrderNo());
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        record.setUpdateId(MoJiCommonUtils.getUserCode());
        record.setCreateId(MoJiCommonUtils.getUserCode());
        return this.save(record);
    }

    @Override
    public boolean edit(SysRole sysRole) {
        LambdaQueryWrapper<SysRole> ccpSystemRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ccpSystemRoleLambdaQueryWrapper.eq(SysRole::getRoleCode,sysRole.getRoleCode());
        sysRole.setUpdateTime(new Date());
        sysRole.setUpdateId(MoJiCommonUtils.getUserCode());
        return this.updateById(sysRole);
    }

    @Override
    public boolean delete(String roleCode) {
        if(MoJiEnumConfig.TGW_SUPPER_ADMINISTRATORS_CODES.getCode().equals(roleCode)){
            throw new RuntimeException("超级管理员角色不允许删除,请核查!");
        }
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getRoleCode,roleCode);

        //角色管理_被引用的角色不可删除
        LambdaQueryWrapper<SysUser> ccpSystemUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ccpSystemUserLambdaQueryWrapper.like(SysUser::getRolesCode,roleCode);
        List<SysUser> userList = sysUserService.list(ccpSystemUserLambdaQueryWrapper);
        if(!userList.isEmpty()){
            return false;
        }
        return this.remove(wrapper);
    }

    @Override
    public MyPage<SysRole> queryPage(SysRole ccpSystemRole) {
        LambdaQueryWrapper<SysRole> ccpSystemRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(ccpSystemRole.getRoleName())){
            ccpSystemRoleLambdaQueryWrapper.like(SysRole::getRoleName,ccpSystemRole.getRoleName());
        }
        if(StringUtils.isNotEmpty(ccpSystemRole.getRoleCode())){
            ccpSystemRoleLambdaQueryWrapper.like(SysRole::getRoleCode,ccpSystemRole.getRoleCode());
        }
        if(StringUtils.isNotEmpty(ccpSystemRole.getDeptCode())){
            ccpSystemRoleLambdaQueryWrapper.eq(SysRole::getDeptCode,ccpSystemRole.getDeptCode());
        }
        ccpSystemRoleLambdaQueryWrapper.orderByDesc(SysRole::getOrderNo);
        PageHelper.startPage(ccpSystemRole.getPageNum(), ccpSystemRole.getPageSize());
        List<SysRole> roleList = this.list(ccpSystemRoleLambdaQueryWrapper);
        PageInfo<SysRole> info = new PageInfo<>(roleList);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public List<RoleSelectBoxDto> queryRoleSelectBox() {
        List<RoleSelectBoxDto> roleSelectBoxlist = new ArrayList<>();
        LambdaQueryWrapper<SysRole> ccpSystemRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<SysRole> roleList = this.list(ccpSystemRoleLambdaQueryWrapper);
        RoleSelectBoxDto roleSelectBoxDao = null;
        if(!roleList.isEmpty()){
            for (SysRole ccpSystemRole : roleList){
                roleSelectBoxDao = new RoleSelectBoxDto();
                roleSelectBoxDao.setRolesCode(ccpSystemRole.getRoleCode());
                roleSelectBoxDao.setRoleName(ccpSystemRole.getRoleName());
                roleSelectBoxlist.add(roleSelectBoxDao);
            }
        }
        return roleSelectBoxlist;
    }

    @Override
    public List<TreeDto> queryRoleTree() {
        LambdaQueryWrapper<SysRole> ccpSystemRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<SysRole> roleList = this.list(ccpSystemRoleLambdaQueryWrapper);
        return RoleTreeUtil.list2Tree(roleList);
    }

    @Override
    public boolean updateRoleStatus(PrOnOffDto prOnOffDao) {
        SysRole sysRole = this.getById(prOnOffDao.getId());
        if(sysRole != null){
            sysRole.setPrOnoff(Integer.parseInt(prOnOffDao.getPrOnOff()));
            sysRole.setPrOnoffTime(new Date());
        }
        return this.updateById(sysRole);
    }
}
